$(function() {

	var defaultPagingObj = {
		total: 20,
		pageSize: 10,
		page: 1
	};

	PAGE_DATA.pagingObj = $.extend(defaultPagingObj, PAGE_DATA.pagingObj);
	// 主要分类：咖啡豆及咖啡相关,等.
	commTemplUtil.initPage();

	// 渲染左侧菜单;
	commTemplUtil.initCommLeftMemu();

	initialize(1);
	//初始化获取列表
	function initialize(page) {
		var onClickObj = {
			onClick: function() {
				var clickPage = $(this).attr("data-page");
				initialize(clickPage);
			}
		};

		var tmplObj = _.template($('#item-template').html());
		$.ajax({
			url: PAGE_CONFIG.requestUrl.counterenquiry ,
			type: "get",
			dataType: "json",
			data: {
				page: page + "",
				pageSize: PAGE_DATA.pagingObj.pageSize,
			},
			success: function(result) {
				var datas = $.extend([], result.data);
				var tmplObjHtml = tmplObj(datas);
				$("#commTabs_1_body").html(tmplObjHtml);
				template1();
				counterOfferDel();
				var tmpConf = $.extend(result.pagingConfData, onClickObj)
				commTemplUtil.initPaging("#comm-paging", tmpConf);
			},
			error: function(xhr, status, error) {
				var tmplObjHtml = tmplObj([]);
				$("#commTabs_1_body").html(tmplObjHtml);
			}
		});
	};
	//删除的事件
	function counterOfferDel() {
		var tmplObj = _.template($('#item-template').html());
		$(".sel-edit-del").click(function() {
			var dataId = $(this).parents("ul").attr("data-id");
			var currentPage = $("#comm-paging").attr("current-page");

			var deleDialog = dialog({
				id: "del-classify-dialog",
				width: 300,
				height: 100,
				title: "注意！",
				content: '确认删除此条数据？',
				ok: doDele,
				cancel: function() {},
				cancelValue: "取消",
				okValue: "确定",
			});

			deleDialog.showModal();
			//删除弹出框的事件
			function doDele() {
				$.ajax({
					url: PAGE_CONFIG.requestUrl.counterenquiry2,
					type: "delete",
					data: {
						id: 111,
						_token: COMM_DATA.csrfToken
					},
					success: function(result) {
						var datas = $.extend([], result.data);
						var tmplObjHtml = tmplObj(datas);
						$("#commTabs_1_body").html(tmplObjHtml);
						initialize(1);
					},
					error: function() {

						var deleFailDialog = dialog({
							id: "del-classify-dialog",
							content: '删除失败。。。',
							ok: function() {},
							okValue: "确定",
						});
						deleFailDialog.showModal();
					}
				});
			};

			return false;
		});
	};

	//弹出框的事件方法
	function template1() {
		var tmplObj1 = _.template($('#item-template1').html());
		var tmplObj = _.template($('#item-template').html());
		//弹出框
		$(".handle").click(function() {
			var getId = $(this).attr("code");
			var dia = dialog({
				title: '   ',
				content: tmplObj1
			});
			dia.showModal();
			$("#submit").attr("code", getId);
			$("#submit").click(function() {
				var thisId = $(this).attr("code");
				$.ajax({
					url: PAGE_CONFIG.requestUrl.counterenquiry1,
					type: "put",
					dataType: "json",
					data: {
						id: 111,
						_token: COMM_DATA.csrfToken,
					},
					success: function(result) {
						datas = $.extend([], result.data);
						var tmplObjHtml = tmplObj(datas);
						$("#commTabs_1_body").html(tmplObjHtml);
						dia.close();
					},
					error: function(xhr, status, error) {
						var tmplObjHtml = tmplObj([]);
						$("#commTabs_1_body").html(tmplObjHtml);
					}
				});
			});
			$(".remove").click(function() {
				dia.close();
			});
		});
	};
});